import { reqGoodsInfo, reqAddorUpdateInfo } from '@/api'
import { getUUID } from '@/utils/uuid_token'
// home模块的小仓库
// state：仓库存储数据的地方
const state = {
    GoodsInfo: {},
    // 游客临时身份
    uuid_token: getUUID()
}
// mutations:修改state的唯一手段
const mutations = {
    GoodsInfo(state, val) {
        state.GoodsInfo = val
    }
}
// actions：处理action，可以书写自己的业务逻辑，也可以处理异步
const actions = {
    // 通过API里面的接口函数调用，向服务器发请求，获取服务器的数据
    async GoodsInfo({ commit }, id) {
        let result = await reqGoodsInfo(id)
        console.log(result)
        if (result.code == 200) {
            console.log(result)
            commit('GoodsInfo', result.data)
        }
    },
    async AddorUpdateInfo({ commit }, { skuId, skuNum }) {
        let result = await reqAddorUpdateInfo( skuId, skuNum )
        console.log(result)
    }
}
// getters:理解为计算属性，用于简化仓库数据，让组件获取仓库的数据更加方便
const getters = {
    categoryView(state) {
        return state.GoodsInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.GoodsInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.GoodsInfo.spuSaleAttrList || []
    }
}
export default ({
    state,
    mutations,
    actions,
    getters
})